package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyMatchingRecordAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.MyMatchingRecordData;
import com.kkd.kkdapp.http.entity.MyMatchingRecordDataItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class MyMatchingRecordActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private Context mContext = this;

	private final int CODE_ZERO = 1;
	private final int CODE_ELSE = 2;
	private long pageNO = 1;
	private long pageSize = 20;

	private ImageView image_null;
	private TextView tv_null;
	private KkdXlistView mXListView;
	private ListView mListView;
	private boolean REFRESHFLAG = true;
	private String financeId;
	private String orderId;
	private MyMatchingRecordData data;
	private List<MyMatchingRecordDataItem> mDataList = new ArrayList<MyMatchingRecordDataItem>();
	private MyMatchingRecordAdapter mAdapter;
	private long mPageNo;
	private long mPageCount;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				if (mPageNo > mPageCount) {
					mXListView.stopLoadMore(-1);
					if (REFRESHFLAG) {
						mXListView.stopLoadMore(1);
					}
				} else {
					mXListView.setNoMore(false);
					if (pageNO == 1) {
						mDataList.clear();
					}
					if (data.getCreditors() == null)
						return;
					mDataList.addAll(data.getCreditors());
					mAdapter.notifyDataSetChanged();

					if (data.getCreditors().size() < pageSize) {
						mXListView.stopLoadMore(1);
					} else {
						mXListView.stopLoadMore(0);
					}
				}
				mXListView.stopRefresh();
				mXListView.initState();
				if (REFRESHFLAG) {
					if (mDataList.size() == 0) {
						image_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
						tv_null.setText("暂无记录");
						image_null.setImageResource(R.drawable.img_null);
					} else {
						tv_null.setVisibility(View.GONE);
						image_null.setVisibility(View.GONE);
					}
				}
				break;

			case CODE_ELSE:
				String desc = (String) msg.obj;
				AppUtil.makeToastForCustomer(mContext, desc);
				mXListView.stopLoadMore(1);
				mXListView.stopRefresh();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_matching_record);
		getMessage();
		initView();
		initListener();
		requestData();
	}

	private void getMessage() {
		financeId = getIntent().getStringExtra("financeId");
		orderId = getIntent().getStringExtra("orderId");
	}

	private void initView() {
		image_null = (ImageView) findViewById(R.id.image_null);
		tv_null = (TextView) findViewById(R.id.tv_null);
		mXListView = (KkdXlistView) findViewById(R.id.xlv);
		mXListView.setKKDScrollListener(this);
		mListView = mXListView.mListView;
		mListView.setDividerHeight(0);
		mAdapter = new MyMatchingRecordAdapter(mContext, mDataList);
		mListView.setAdapter(mAdapter);
		mXListView.startRefresh();
	}

	private void initListener() {
		findViewById(R.id.td_top_img).setOnClickListener(this);// 退出
		findViewById(R.id.agreement_bt).setOnClickListener(this);// 用户协议
	}

	/*
	 * 请求债券匹配记录
	 */
	private void requestData() {
		mXListView.setNoMore(false);
		KkdHttpMagager.getInstance().getMyMatchingRecordManager(orderId,
				pageNO, pageSize, MyMatchingRecordData.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						data = (MyMatchingRecordData) responseBody;
						mPageNo = data.getPageNO();
						mPageCount = data.getPageCount();
						mHandler.sendEmptyMessage(CODE_ZERO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.obj = desc;
						msg.what = CODE_ELSE;
						mHandler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.td_top_img:// 退出
			finish();
			break;

		case R.id.agreement_bt:// 用户协议
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		requestData();
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		requestData();
	}
}
